<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="warehouseInOutOrderNum">
        <el-input
          v-model="queryParams.warehouseInOutOrderNum"
          placeholder="请输入进出仓单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="purchaseOrderNumber">
        <el-input
          v-model="queryParams.purchaseOrderNumber"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="warehouseEntryTime">
        <el-date-picker
          clearable
          v-model="queryParams.warehouseEntryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入仓时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="inventoryQuantity">
        <el-input
          v-model="queryParams.inventoryQuantity"
          placeholder="请输入出入库数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="" prop="inOutWarehouseAmount">
        <el-input
          v-model="queryParams.inOutWarehouseAmount"
          placeholder="请输入进出仓金额(含税)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="" prop="externalOrderNumber">
        <el-input
          v-model="queryParams.externalOrderNumber"
          placeholder="请输入外部单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="dataDate">
        <el-date-picker
          clearable
          v-model="queryParams.dataDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <el-row>
      <el-col :span="9">
        <timeSearch
          @getDateArr="getDateArr"
          @changDay="changDay"
          :timeArr="timeArr"
          :dayActive="this.dayActive"
          :dayLimit="1095"
        />
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
          >同步</el-button
        >
      </el-col>
    </el-row> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:BiWarehouseDateDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:BiWarehouseDateDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:BiWarehouseDateDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:BiWarehouseDateDwd:export']"
          >导出</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleinit">数据同步</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button plain size="mini" @click="handleautoSynData"
          >一键同步</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :height="600"
      v-loading="loading"
      :data="BiWarehouseDateDwdList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品日期数据id" align="center" prop="id" />
      <!-- <el-table-column
        label="进出仓单号"
        align="center"
        prop="warehouseInOutOrderNum"
      />
      <el-table-column
        label="采购单号"
        align="center"
        prop="purchaseOrderNumber"
      />
      <el-table-column label="单据类型" align="center" prop="documentType" /> -->
      <el-table-column
        label="入仓时间"
        align="center"
        prop="warehouseEntryTime"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.warehouseEntryTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="180"
        label="商品编号"
        align="center"
        prop="productCode"
      />
      <el-table-column
        label="出入库数量"
        align="center"
        prop="inventoryQuantity"
      />
      <el-table-column
        label="进出仓金额(含税)"
        align="center"
        prop="inOutWarehouseAmount"
      />
      <!-- <el-table-column label="备注" align="center" prop="remarks">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="bottom" width="300">
            <el-input
              v-model="scope.row.remarks"
              :min-height="192"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 24 }"
              :style="{ width: '100%' }"
              :disabled="true"
            />
            <div slot="reference">
              <span class="textname">{{ scope.row.remarks }} </span>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="外部单号"
        align="center"
        prop="externalOrderNumber"
      /> -->
      <el-table-column label="日期" align="center" prop="dataDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:BiWarehouseDateDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:BiWarehouseDateDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库日期存储情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="进出仓单号" prop="warehouseInOutOrderNum">
          <el-input
            v-model="form.warehouseInOutOrderNum"
            placeholder="请输入进出仓单号"
          />
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item>
        <el-form-item label="入仓时间" prop="warehouseEntryTime">
          <el-date-picker
            clearable
            v-model="form.warehouseEntryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入仓时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="出入库数量" prop="inventoryQuantity">
          <el-input
            v-model="form.inventoryQuantity"
            placeholder="请输入出入库数量"
          />
        </el-form-item>
        <el-form-item label="进出仓金额(含税)" prop="inOutWarehouseAmount">
          <el-input
            v-model="form.inOutWarehouseAmount"
            placeholder="请输入进出仓金额(含税)"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="外部单号" prop="externalOrderNumber">
          <el-input
            v-model="form.externalOrderNumber"
            placeholder="请输入外部单号"
          />
        </el-form-item>
        <el-form-item label="日期" prop="dataDate">
          <el-date-picker
            clearable
            v-model="form.dataDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiWarehouseDateDwd,
  getBiWarehouseDateDwd,
  delBiWarehouseDateDwd,
  addBiWarehouseDateDwd,
  updateBiWarehouseDateDwd,
  synData,
  initData,
  autoSynData,
} from "@/api/Warehouse/BiWarehouseDateDwd";
import timeSearch from "@/common/day";
export default {
  name: "BiWarehouseDateDwd",
  components: { timeSearch },
  data() {
    return {
      dayActive: 2,
      timeArr: [
        { value: "2", label: "昨天" },
        { value: "7", label: "最近7天" },
        { value: "30", label: "最近30天" },
        { value: "365", label: "最近一年" },
        { value: "1", label: "日" },
      ],
      timeGap: { startTime: "", endTime: "" },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库日期存储情况表格数据
      BiWarehouseDateDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "商品编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //初始化数据
    handleinit() {
      this.openLoading();
      initData().then((response) => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess("已初始化数据");
        this.getList();
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在同步数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefresh() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      const obj = {
        startTime: this.timeGap.startTime,
        endTime: this.timeGap.endTime,
      };
      this.openLoading();
      synData(obj).then(() => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    handleautoSynData() {
      this.openLoading();
      autoSynData().then(() => {
        this.$modal.closeLoading();
        this.$modal.msgSuccess("同步数据成功");
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    //时间控件
    changDay(val) {
      this.dayActive = val;
      this.timeGap.startTime = val[0];
      this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      this.timeGap.startTime = value[0];
      this.timeGap.endTime = value[1];
      console.log(JSON.stringify(this.timeGap));
      this.dayActive = 1;
    },
    /** 查询仓库日期存储情况列表 */
    getList() {
      this.loading = true;
      listBiWarehouseDateDwd(this.queryParams).then((response) => {
        this.BiWarehouseDateDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库日期存储情况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiWarehouseDateDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库日期存储情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiWarehouseDateDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiWarehouseDateDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除仓库日期存储情况编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiWarehouseDateDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "financial/BiWarehouseDateDwd/export",
        {
          ...this.queryParams,
        },
        `BiWarehouseDateDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}

// ::v-deep input[type='number'] {
//   // -moz-appearance: textfield !important;
// }

.el-dialog-div {
  height: 50vh;
  overflow-x: hidden;
}

span.textname {
  display: -webkit-box;

  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  white-space: pre-wrap;
}

.el-input.is-disabled ::v-deep .el-input__inner {
  color: #606266;
  background-color: white;
  border: none;
}

.el-textarea.is-disabled ::v-deep .el-textarea__inner {
  color: #606266;
  background-color: white;
  border: none;
  width: 500px;
  resize: none;
  cursor: pointer;
}
</style>
